﻿using System;
using System.Windows.Forms;
using RibbonEditor.Forms.LocLabels;
using RibbonEditor.Forms.WebRessources;
using System.Xml;
using RibbonEditor.Forms.Rules;
using RibbonEditor.Forms.CommandDefinitions;
using RibbonEditor.Forms.Publishers;
using RibbonEditor.Forms.Solutions;

namespace RibbonEditor.Controls
{
    public partial class ReferencedControl : UserControl, IDirtable
    {
        bool firstAccess = true;

        string initialValue = null;

        public bool IsDirty
        {
            set { if (value == false) this.initialValue = this.txtText.Text; }
            get { return this.isDirty; }
        }

        private bool isDirty;

        public string Label
        {
            get
            {
                return this.lblLabel.Text;
            }
            set
            {
                this.lblLabel.Text = value;
            }
        }

        public string Value
        {
            get
            {
                return this.txtText.Text;
            }
            set
            {
                if (this.initialValue == null)
                    this.initialValue = value;
                
                this.txtText.Text = value;
            }
        }

        public bool IsRequired
        {
            get
            {
                return this.lblRequired.Visible;
            }
            set
            {
                this.lblRequired.Visible = value;
            }
        }

        public XmlDocument RibbonXml { get; set; }

        public string Prefix { get; set; }

        public bool IsReferencedItem 
        { 
            get { return this.Value.StartsWith("$"); } 
        }

        bool isEditable = true;

        public bool IsEditable
        {
            get { return isEditable; }
            set { isEditable = value; }
        }

        public ReferencedControl()
        {
            //this.txtText.TextChanged -= new System.EventHandler(this.txtText_TextChanged);

            InitializeComponent();

            this.txtText.Enabled = isEditable;
            this.btnBrowse.Enabled = isEditable;

            this.txtText.TextChanged += new System.EventHandler(this.txtText_TextChanged);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            string result = string.Empty;
            string resultPrefix = string.Empty;

            if (this.Prefix == "LocLabels")
            {
                XmlNode locLabelsNode = MainForm.currentRibbon.SelectSingleNode("LocLabels");

                LocLabelPicker picker = new LocLabelPicker(locLabelsNode, ((MainForm)this.ParentForm));
                picker.StartPosition = FormStartPosition.CenterParent;

                if (picker.ShowDialog() == DialogResult.OK)
                {
                    result = picker.SelectedLocLabel;
                    resultPrefix = this.Prefix;
                }
            }
            else if (this.Prefix == "webresource")
            {
                XmlNode webResourcesNode = MainForm.sManager.CustomizationFile.SelectSingleNode("//WebResources");

                WebResourcePicker picker = new WebResourcePicker(webResourcesNode);
                picker.StartPosition = FormStartPosition.CenterParent;

                if (picker.ShowDialog() == DialogResult.OK)
                {
                    result = picker.SelectedResource;
                    resultPrefix = this.Prefix;
                }
            }
            else if (this.Prefix == "EnableRule")
            {
                XmlNode enableRulesNode = MainForm.currentRibbon.SelectSingleNode("RuleDefinitions/EnableRules");
                
                RulePicker picker = new RulePicker(enableRulesNode, ((MainForm)this.ParentForm));
                picker.StartPosition = FormStartPosition.CenterParent;

                if (picker.ShowDialog() == DialogResult.OK)
                {
                    result = picker.SelectedRule;
                    resultPrefix = string.Empty;
                }
            }
            else if (this.Prefix == "DisplayRule")
            {
                XmlNode enableRulesNode = MainForm.currentRibbon.SelectSingleNode("RuleDefinitions/DisplayRules");

                RulePicker picker = new RulePicker(enableRulesNode, ((MainForm)this.ParentForm));
                picker.StartPosition = FormStartPosition.CenterParent;

                if (picker.ShowDialog() == DialogResult.OK)
                {
                    result = picker.SelectedRule;
                    resultPrefix = string.Empty;
                }
            }
            else if (this.Prefix == "command")
            {
                XmlNode commandsNode = MainForm.currentRibbon.SelectSingleNode("CommandDefinitions");

                CommandSelectionPicker picker = new CommandSelectionPicker(commandsNode);
                picker.StartPosition = FormStartPosition.CenterParent;

                if (picker.ShowDialog() == DialogResult.OK)
                {
                    result = picker.SelectedCommandDefinition;
                    resultPrefix = string.Empty;
                }
            }
            else if (this.Prefix == "template")
            {
                XmlNode tempaltesNode = MainForm.currentRibbon.SelectSingleNode("Templates");

                CommandSelectionPicker picker = new CommandSelectionPicker(tempaltesNode);
                picker.StartPosition = FormStartPosition.CenterParent;

                if (picker.ShowDialog() == DialogResult.OK)
                {
                    result = picker.SelectedCommandDefinition;
                    resultPrefix = string.Empty;
                }
            }
            else if (this.Prefix == "publisher")
            {
                PublisherPicker picker = new PublisherPicker(MainForm.service);
                picker.StartPosition = FormStartPosition.CenterParent;

                if (picker.ShowDialog() == DialogResult.OK)
                {
                    resultPrefix = string.Empty;
                    result = picker.SelectedPublisher["friendlyname"].ToString();

                    ((CreateSolutionDialog)this.Parent.Parent).publisherId = (Guid)picker.SelectedPublisher["publisherid"];
                }
            }

            if (result.Length > 0)
            {
                if (string.IsNullOrEmpty(resultPrefix))
                {
                    this.txtText.Text = result;
                }
                else
                {
                    this.txtText.Text = string.Format("${0}:{1}", resultPrefix, result);
                }
            }
        }

        private void txtText_TextChanged(object sender, EventArgs e)
        {
            if (!firstAccess)
            {
                this.IsDirty = this.initialValue != this.txtText.Text;
            }
            else
            {
                firstAccess = false;
            } 
            
            if (this.Prefix == "publisher" && this.txtText.Text.Length == 0)
            {
                ((CreateSolutionDialog)this.Parent.Parent).publisherId = Guid.Empty;
            }
        }

        public void ReinitValues()
        {
            this.txtText.Text = this.initialValue.ToString();
            this.isDirty = false;
        }
    }
}
